/*
@title: Mancala
@description: Mancala, also known as Bantumi, is a strategic two-player board game involving the distribution of stones across a series of pits with the aim of capturing the most pieces. The game is steeped in strategy, requiring players to wisely select pits for sowing seeds anticlockwise, with the potential for additional turns and strategic captures. This retro-styled multiplayer version presents a classic rendition of this time-honored game.
@author: Lenochodik
@tags: ['puzzle','strategy','multiplayer','retro']
@addedOn: 2024-08-06
*/

// = Types =========================================
const pit = "p"
const storeUp = "1"
const storeDown = "2"
const half1 = "3"
const half2 = "4"
const nothing = "5"

const handUp = "q"
const handDown = "w"
const handUpClosed = "r"
const handDownClosed = "t"

const tileTopLeft = "a"
const tileTopRight = "b"
const tileTopRightShine = "l"
const tileBotLeft = "c"
const tileBotLeftShine = "m"
const tileBotRight = "d"
const tileTop = "e"
const tileTopShine = "k"
const tileBot = "f"
const tileBotShine = "n"
const tileLeft = "g"
const tileLeftShine = "o"
const tileRight = "h"
const tileRightShine = "j"
const tileBrown = "i"

const num0_R = "A"
const num1_R = "B"
const num2_R = "C"
const num3_R = "D"
const num4_R = "E"
const num5_R = "F"
const num6_R = "G"
const num7_R = "H"
const num8_R = "I"
const num9_R = "J"

const num0_L = "K"
const num1_L = "L"
const num2_L = "M"
const num3_L = "N"
const num4_L = "O"
const num5_L = "P"
const num6_L = "Q"
const num7_L = "R"
const num8_L = "S"
const num9_L = "T"
// =================================================

// = Legends =======================================
setLegend(
  // Custom font
  // - right
  [num0_R, bitmap`
................
................
................
................
..........666...
.........6...6..
.........6...6..
.........6...6..
.........6...6..
.........6...6..
.........6...6..
..........666...
................
................
................
................`],
  [num1_R, bitmap`
................
................
................
................
...........6....
..........66....
.........6.6....
...........6....
...........6....
...........6....
...........6....
.........66666..
................
................
................
................`],
  [num2_R, bitmap`
................
................
................
................
..........66....
.........6..6...
............6...
............6...
...........6....
..........6.....
.........6......
.........6666...
................
................
................
................`],
  [num3_R, bitmap`
................
................
................
................
..........66....
.........6..6...
............6...
..........66....
............6...
............6...
.........6..6...
..........66....
................
................
................
................`],
  [num4_R, bitmap`
................
................
................
................
...........6....
..........66....
.........6.6....
.........6.6....
.........6666...
...........6....
...........6....
...........6....
................
................
................
................`],
  [num5_R, bitmap`
................
................
................
................
.........6666...
.........6......
.........6......
.........666....
............6...
............6...
.........6..6...
..........66....
................
................
................
................`],
  [num6_R, bitmap`
................
................
................
................
..........66....
.........6..6...
.........6......
.........6......
.........666....
.........6..6...
.........6..6...
..........66....
................
................
................
................`],
  [num7_R, bitmap`
................
................
................
................
.........6666...
.........6..6...
............6...
...........6....
...........6....
..........6.....
..........6.....
..........6.....
................
................
................
................`],
  [num8_R, bitmap`
................
................
................
................
..........666...
.........6...6..
.........6...6..
..........666...
.........6...6..
.........6...6..
.........6...6..
..........666...
................
................
................
................`],
  [num9_R, bitmap`
................
................
................
................
..........66....
.........6..6...
.........6..6...
..........666...
............6...
............6...
.........6..6...
..........66....
................
................
................
................`],
  // - left
  [num0_L, bitmap`
................
................
................
................
...666..........
..6...6.........
..6...6.........
..6...6.........
..6...6.........
..6...6.........
..6...6.........
...666..........
................
................
................
................`],
  [num1_L, bitmap`
................
................
................
................
....6...........
...66...........
..6.6...........
....6...........
....6...........
....6...........
....6...........
..66666.........
................
................
................
................`],
  [num2_L, bitmap`
................
................
................
................
....66..........
...6..6.........
......6.........
......6.........
.....6..........
....6...........
...6............
...6666.........
................
................
................
................`],
  [num3_L, bitmap`
................
................
................
................
....66..........
...6..6.........
......6.........
....66..........
......6.........
......6.........
...6..6.........
....66..........
................
................
................
................`],
  [num4_L, bitmap`
................
................
................
................
.....6..........
....66..........
...6.6..........
...6.6..........
...6666.........
.....6..........
.....6..........
.....6..........
................
................
................
................`],
  [num5_L, bitmap`
................
................
................
................
...6666.........
...6............
...6............
...666..........
......6.........
......6.........
...6..6.........
....66..........
................
................
................
................`],
  [num6_L, bitmap`
................
................
................
................
....66..........
...6..6.........
...6............
...6............
...666..........
...6..6.........
...6..6.........
....66..........
................
................
................
................`],
  [num7_L, bitmap`
................
................
................
................
...6666.........
...6..6.........
......6.........
.....6..........
.....6..........
....6...........
....6...........
....6...........
................
................
................
................`],
  [num8_L, bitmap`
................
................
................
................
...666..........
..6...6.........
..6...6.........
...666..........
..6...6.........
..6...6.........
..6...6.........
...666..........
................
................
................
................`],
  [num9_L, bitmap`
................
................
................
................
....66..........
...6..6.........
...6..6.........
....666.........
......6.........
......6.........
...6..6.........
....66..........
................
................
................
................`],
  // Basics
  /* [pit, bitmap`
....11111122....
...1000000002...
..100000000002..
.10000000000001.
1000000000000001
1000000L10000002
100000LLL1000002
10000LLLLL100001
10000LLLLL100001
100000LLLL000001
1000000LL0000001
1000000000000001
.10000000000001.
..100000000001..
...1000000001...
....11111111....`],*/
  [pit, bitmap`
....11111122....
...1000000002...
..100000000002..
.10000000000001.
100002L1LLL00001
10002LLLLLLL0002
10002LLLLLLL0002
1000LLLLLLLL0001
10002LLLLLLL0001
1000LLLLLLLL0001
1000LLLLLLLL0001
10000LLLLLL00001
.10000000000001.
..100000000001..
...1000000001...
....11111111....`],
  [storeUp, bitmap`
....11111111....
...1000000002...
..100000000002..
.10000000000002.
1000000000000002
10000LLLLLL00002
1000LL22LLLL0002
100LL2LLLLLLL001
100L1LLLLLLLL001
100L1LLLLLLLL001
100LLLLLLLLLL001
100L1LLLLLLLL001
100LLLLLLLLLL001
100LLLLLLLLLL001
100LLLLLLLLLL001
100LLLLLLLLLL001`],
  [storeDown, bitmap`
100L1LLLLLLLL002
100LLLLLLLLLL001
100LLLLLLLLLL002
100LLLLLLLLLL002
100LLLLLLLLLL001
100L1LLLLLLLL001
100L1LLLLLLLL001
100LLLLLLLLLL001
100LL2LLLLLLL001
1000LL1LLLLL0001
10000LLLLLL00001
1000000000000001
.10000000000001.
..100000000001..
...1000000001...
....11111111....`],
  [half1, bitmap`
0000000000000000
1111111111111111
1111111111211222
................
................
................
................
................
................
................
................
................
................
................
................
................`],
  [half2, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
1111111111222121
1111111111111111
0000000000000000`],
  [nothing, bitmap`
99C99999999CCC99
C9CC99CCCC9C99CC
CCCCC99CCC9C9CCC
C9CCCC9CC9CC9CCC
C9C9999CC9CC9999
9CC9CCCCC9CCCCC9
CCC9CC9999999CCC
999999CC99C999CC
CC9CCCCCC99CC999
CC9CCC999C9CCC9C
C9999C9C99C999CC
CCCC9C9CC99CC99C
CC999C9CCC9CC99C
C99CCC9CCC9CC9CC
C9CCCC9CCC9999CC
CC99999CCCCCCCC9`],
  // Hands
  [handUp, bitmap`
.....00.........
....0220........
....0220........
....022000......
....0220120.....
....022012000...
....02201201100.
000.0220L2012010
0110022L120L20L0
021L022222L120L0
002L021212222L20
.001L22121212220
..0012222212120.
...012222222210.
....0L1111111L0.
....0LLLLLLLL0..`],
  [handDown, bitmap`
..0LLLLLLLL0....
.0L1111111L0....
.012222222210...
.0212122222100..
02221212122L100.
02L222212120L200
0L021L222220L120
0L02L021L2200110
0102102L0220.000
.00110210220....
...000210220....
.....0210220....
......000220....
........0220....
........0220....
.........00.....`],
  [handUpClosed, bitmap`
.......000......
.....000120.....
....022012000...
....02201201100.
000.0220L2012010
0110022L120L20L0
021L022222L120L0
002L021212222L20
.001L22121212220
..0012222212120.
...012222222210.
....0L1111111L0.
....0LLLLLLLL0..
................
................
................`],
  [handDownClosed, bitmap`
................
................
................
..0LLLLLLLL0....
.0L1111111L0....
.012222222210...
.0212122222100..
02221212122L100.
02L222212120L200
0L021L222220L120
0L02L021L2200110
0102102L0220.000
.00110210220....
...000210220....
.....021000.....
......000.......`],
  // Tiles
  [tileTopLeft, bitmap`
................
................
................
................
................
................
................
................
............1111
..........111CCC
.........11CCCCC
.........1CCCCCC
........11CCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC`],
  [tileTopRight, bitmap`
................
................
................
................
................
................
................
................
1111............
CCC111..........
CCCCC11.........
CCCCCC1.........
CCCCCC11........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........`],
  [tileTopRightShine, bitmap`
................
................
................
................
................
................
................
................
1122............
CCC211..........
CCCCC22.........
CCCCCC2.........
CCCCCC11........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........`],
  [tileTop, bitmap`
................
................
................
................
................
................
................
................
1111111111111111
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
  [tileTopShine, bitmap`
................
................
................
................
................
................
................
................
1111212222222222
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
  [tileBotLeft, bitmap`
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........11CCCCCC
.........1CCCCCC
.........11CCCCC
..........111CCC
............1111
................
................
................
................
................
................
................
................`],
  [tileBotLeftShine, bitmap`
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........11CCCCCC
.........2CCCCCC
.........22CCCCC
..........112CCC
............2211
................
................
................
................
................
................
................
................`],
  [tileBotRight, bitmap`
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCC11........
CCCCCC1.........
CCCCC11.........
CCC111..........
1111............
................
................
................
................
................
................
................
................`],
  [tileBot, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
1111111111111111
................
................
................
................
................
................
................
................`],
  [tileBotShine, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
2222222222121111
................
................
................
................
................
................
................
................`],
  [tileLeft, bitmap`
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC`],
  [tileLeftShine, bitmap`
........1CCCCCCC
........1CCCCCCC
........1CCCCCCC
........2CCCCCCC
........1CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC
........2CCCCCCC`],
  [tileRight, bitmap`
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........
CCCCCCC1........`],
  [tileRightShine, bitmap`
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC1........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC2........
CCCCCCC1........`],
  [tileBrown, bitmap`
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC
CCCCCCCCCCCCCCCC`],
)
// =================================================

// = Levels ========================================
let level = 0
const levels = [
  map`
..........
aeeeeeekkl
g.pppppp.j
g14444441j
o23333332h
o.pppppp.h
mnnffffffd
..........`
]
setMap(levels[level])
// =================================================

// = Melodies, sounds ==============================
const soundMoveHand = tune`
300: B4^300,
9300`
const soundMoveHandOutOfBounds = tune`
300: A4^300 + F4~300 + G4~300 + B4~300 + C5~300,
9300`
const soundPutPieceOdd = tune`
300: G4~300 + B4/300,
9300`
const soundPutPieceEven = tune`
300: A4~300 + C5/300,
9300`
const soundCollectOpponentsPieces = tune`
150: B4^150,
150: C5^150,
150: D5/150,
150: B4^150,
150: D5/150,
4050`
const soundPlayAgain = tune`
93.75: B4^93.75,
93.75: C5^93.75,
93.75: D5^93.75,
93.75: E5^93.75,
93.75: F5^93.75,
93.75: G5^93.75,
93.75: A5^93.75,
93.75: B5^93.75,
2250`
// =================================================

// = Custom font functions =========================
const customFont = [
  [num0_R, num1_R, num2_R, num3_R, num4_R, num5_R, num6_R, num7_R, num8_R, num9_R],
  [num0_L, num1_L, num2_L, num3_L, num4_L, num5_L, num6_L, num7_L, num8_L, num9_L],
]

function addCustomText(str, x, y, bgSprite = pit) {
  clearTile(x, y)
  addSprite(x, y, tileBrown)
  addSprite(x, y, bgSprite)
  const strReversed = str.split('').reverse().join('')
  for (let i = 0; i < str.length && i < customFont.length; i++)
    addSprite(x, y, customFont[i][parseInt(strReversed.charAt(i), 10)])
}
// =================================================


// = Mancala code ==================================
const halfBoardSize = 6
const initialPieces = 4
const storesCount = 2
const storesIndexes = [halfBoardSize, 2 * halfBoardSize + 1]
let game = {
  board: Array(halfBoardSize * 2 + storesCount)
    .fill(initialPieces),
  currentPlayer: 0,
}
game.board[storesIndexes[0]] = 0
game.board[storesIndexes[1]] = 0

const boardTopY = 2
const boardBotY = 5
const boardX = 2

const storeLeftX = 1
const storeRightX = 8
const storeY = 4

const handMinX = boardX
const handMaxX = boardX + halfBoardSize - 1
let objectHand = null

let loopInterval = null

function printBoardText() {
  // Bottom half
  for (let i = 0; i < halfBoardSize; i++)
    addCustomText(game.board[i].toString(), boardX + i, boardBotY)

  // Top half
  for (let i = 0; i < halfBoardSize; i++)
    addCustomText(game.board[game.board.length - 2 - i].toString(), boardX + i, boardTopY)

  // Left store
  addCustomText(game.board[storesIndexes[1]].toString(), storeLeftX, storeY, storeDown)

  // Right store
  addCustomText(game.board[storesIndexes[0]].toString(), storeRightX, storeY, storeDown)
}

function prepareHandForSelection() {
  const isBot = game.currentPlayer === 0

  objectHand?.remove()
  isBot ?
    addSprite(handMinX, boardBotY + 1, handUp) :
    addSprite(handMaxX, boardTopY - 1, handDown)
  objectHand = isBot ? getFirst(handUp) : getFirst(handDown)

  clearText()
  addText("Your turn", {
    x: width() / 2,
    y: isBot ? 2 * height() - 1 : 0,
    color: color`6`
  })
}

function handCoordsToBoardCoords() {
  const isBot = game.currentPlayer === 0

  if (isBot)
    return objectHand.x - handMinX
  else
    return handMaxX - objectHand.x + (halfBoardSize + 1)
}

// Returns coords in form of: [x, y, sprite]
function boardCoordsToHandCoords(index) {
  if (index >= 0 && index < halfBoardSize)
    return [boardX + index, boardBotY + 1, handUpClosed];
  if (index === storesIndexes[0])
    return [boardX + index, boardBotY, handUpClosed];

  if (index > halfBoardSize && index <= (2 * halfBoardSize))
    return [boardX + halfBoardSize - 1 - (index - halfBoardSize - 1), boardTopY - 1, handDownClosed];
  return [boardX - 1, boardTopY, handDownClosed];
}

function showClosedHand(...indexes) {
  // Remove closed hands used for animation
  const closedHandObjects = [...getAll(handUpClosed), ...getAll(handDownClosed)]
  for (let objectClosedHand of closedHandObjects)
    objectClosedHand.remove()

  if (!indexes.length)
    return

  for (let index of indexes) {
    const [x, y, sprite] = boardCoordsToHandCoords(index)
    addSprite(x, y, sprite)
  }
}

function haveCurrentPlayerAnyMovesLeft() {
  let sum = 0;
  const startIndexByPlayer = game.currentPlayer === 0 ? 0 : halfBoardSize + 1;
  for (let i = 0; i < halfBoardSize; i++)
    sum += game.board[i + startIndexByPlayer]
  return sum > 0
}

async function collectAllPiecesLeft() {
  const startIndexOpposite = game.currentPlayer === 1 ? 0 : halfBoardSize + 1;

  let indexes = []
  for (let i = 0; i < halfBoardSize; i++)
    indexes.push(i + startIndexOpposite)
  showClosedHand(...indexes)

  await delay(1000)

  let temporaryStore = 0
  for (let i = 0; i < halfBoardSize; i++) {
    temporaryStore += game.board[i + startIndexOpposite]
    game.board[i + startIndexOpposite] = 0
  }
  printBoardText()
  playTune(soundCollectOpponentsPieces)
  await delay(1000)


  showClosedHand(storesIndexes[1 - game.currentPlayer])
  game.board[storesIndexes[1 - game.currentPlayer]] += temporaryStore
  printBoardText()
  await delay(1000)

  
}

function printGameStatus() {
  const storeBot = game.board[storesIndexes[0]]
  const storeTop = game.board[storesIndexes[1]]

  clearText()

  if (storeBot === storeTop) {
    addText("It's a draw!", {
      x: width() / 2,
      y: 2 * height() - 1,
      color: color`6`
    })

    addText("It's a draw!", {
      x: width() / 2,
      y: 0,
      color: color`6`
    })

    return
  }

  addText("You win!", {
    x: width() / 2,
    y: storeBot > storeTop ? 2 * height() - 1 : 0,
    color: color`6`
  })
}

function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function startNewGame() {
  setMap(levels[level])
  setBackground(nothing)

  for (let x = storeLeftX; x <= storeRightX; x++)
    for (let y = boardTopY; y <= boardBotY; y++)
      addSprite(x, y, tileBrown)

  game = {
    board: Array(halfBoardSize * 2 + storesCount)
      .fill(initialPieces),
    currentPlayer: 0,
  }
  game.board[storesIndexes[0]] = 0
  game.board[storesIndexes[1]] = 0
  printBoardText()
  prepareHandForSelection()
}
startNewGame()

onInput("a", () => {
  if (!objectHand) return

  if (objectHand.x <= handMinX)
    playTune(soundMoveHandOutOfBounds)
  else {
    playTune(soundMoveHand)
    objectHand.x -= 1
  }
})

onInput("d", () => {
  if (!objectHand) return

  if (objectHand.x >= handMaxX)
    playTune(soundMoveHandOutOfBounds)
  else {
    playTune(soundMoveHand)
    objectHand.x += 1
  }
})

onInput("l", () => {
  clearInterval(loopInterval)
  startNewGame()
})

onInput("k", () => {
  if (!objectHand) return

  let index = handCoordsToBoardCoords()
  let pieces = game.board[index]

  if (pieces <= 0) {
    playTune(soundMoveHandOutOfBounds)
    return
  }

  game.board[index] = 0
  playTune(soundPutPieceOdd)
  printBoardText()

  objectHand.remove()
  objectHand = null

  showClosedHand(index)

  // Loop to spread the pieces
  loopInterval = setInterval(async () => {
    if (pieces <= 0) {
      clearInterval(loopInterval)

      // If it ends up in my store
      if (index === storesIndexes[game.currentPlayer]) {
        playTune(soundPlayAgain)
      } else {
        // Check if I can colllect pieces (ended up on my side in empty hole)
        if ((game.currentPlayer === 0 &&
            index >= 0 && index < halfBoardSize) ||
          (game.currentPlayer === 1 &&
            index > halfBoardSize && index <= (2 * halfBoardSize))) {
          const oppositeIndex = (2 * halfBoardSize) - index


          // Was empty hole before?
          // Does opponent has some pieces in the opposite hole?
          if (game.board[index] === 1 && game.board[oppositeIndex] > 0) {
            showClosedHand(index, oppositeIndex)
            await delay(1000) // wait for animation to be seen
            
            // Temporary store because of animation
            let temporaryStore = 0
            
            // Collect 1 piece on my side
            temporaryStore += game.board[index]
            game.board[index] = 0
            // Collect everything on opposite side (in opposite pit)
            temporaryStore += game.board[oppositeIndex]
            game.board[oppositeIndex] = 0

            // Play sound
            playTune(soundCollectOpponentsPieces)

            // Show animation
            printBoardText()
            await delay(1000) // wait for animation to be seen

            game.board[storesIndexes[game.currentPlayer]] += temporaryStore
            printBoardText()
            showClosedHand(storesIndexes[game.currentPlayer])
            await delay(1000) // wait for animation to be seen
          }
        }

        // Switch player
        game.currentPlayer = 1 - game.currentPlayer
      }

      // Remove closed hands used for animation
      showClosedHand()

      printBoardText()

      // Check for the end of the game (no pieces on current side)
      if (!haveCurrentPlayerAnyMovesLeft()) {
        collectAllPiecesLeft();
        printGameStatus();
      } else
        prepareHandForSelection()

      return
    }

    playTune(soundPutPieceEven)

    index++
    pieces--
    // Skip oponnent's store
    if (index === storesIndexes[1 - game.currentPlayer])
      index++
    index %= game.board.length
    game.board[index]++
    printBoardText()

    showClosedHand(index)
  }, 500)
})